package br.affCorp.controller.persistence;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class ConnectionSingleton {

	public ConnectionSingleton() {
	}

	public static Connection getConnection() throws Exception {
		try {
			Class.forName("oracle.jdbc.driver.OracleDriver");
			return DriverManager.getConnection(
					"jdbc:oracle:thin:@localhost:1521:xe", "admin", "admin");
		} catch (Exception e) {
			throw new Exception(e.getMessage());
		}
	}

	public static void closeConnection(Connection conn, Statement stmt,
			ResultSet rs) {
		close(conn, stmt, rs);
	}

	public static void closeConnection(Connection conn, Statement stmt) {
		close(conn, stmt, null);
	}

	public static void closeConnection(Connection conn) {
		close(conn, null, null);
	}

	private static void close(Connection conn, Statement stmt, ResultSet rs) {
		try {
			if (rs != null)
				rs.close();
			if (stmt != null)
				stmt.close();
			if (conn != null)
				conn.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
